package com.example.myapplication;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.example.myapplication.api.AuthInterceptor;
import com.example.myapplication.api.PasswordApi;
import com.example.myapplication.model.CreatePasswordRequest;
import com.example.myapplication.model.GeneratePasswordRequest;
import com.example.myapplication.model.Password;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.textfield.TextInputEditText;

import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class AddPasswordActivity extends AppCompatActivity {
    private TextInputEditText etTitle;
    private TextInputEditText etWebsite;
    private TextInputEditText etUsername;
    private TextInputEditText etPassword;
    private TextInputEditText etNotes;
    private PasswordApi passwordApi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_password);

        // 设置Toolbar
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }

        // 初始化Retrofit
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(new AuthInterceptor(this))
                .build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://10.0.2.2:8080/")
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        
        passwordApi = retrofit.create(PasswordApi.class);

        // 初始化视图
        etTitle = findViewById(R.id.etTitle);
        etWebsite = findViewById(R.id.etWebsite);
        etUsername = findViewById(R.id.etUsername);
        etPassword = findViewById(R.id.etPassword);
        etNotes = findViewById(R.id.etNotes);

        // 设置生成密码按钮点击事件
        MaterialButton btnGeneratePassword = findViewById(R.id.btnGeneratePassword);
        btnGeneratePassword.setOnClickListener(v -> generatePassword());

        // 设置保存按钮点击事件
        MaterialButton btnSave = findViewById(R.id.btnSave);
        btnSave.setOnClickListener(v -> savePassword());
    }

    private void generatePassword() {
        GeneratePasswordRequest request = new GeneratePasswordRequest(16, true, true, true, true);
        passwordApi.generatePassword(request).enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.isSuccessful() && response.body() != null) {
                    etPassword.setText(response.body());
                } else {
                    Toast.makeText(AddPasswordActivity.this, 
                        "生成密码失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Toast.makeText(AddPasswordActivity.this, 
                    "网络错误", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void savePassword() {
        String title = etTitle.getText().toString().trim();
        String website = etWebsite.getText().toString().trim();
        String username = etUsername.getText().toString().trim();
        String password = etPassword.getText().toString().trim();
        String notes = etNotes.getText().toString().trim();

        if (title.isEmpty() || website.isEmpty() || username.isEmpty() || password.isEmpty()) {
            Toast.makeText(this, "请填写必要信息", Toast.LENGTH_SHORT).show();
            return;
        }

        CreatePasswordRequest request = new CreatePasswordRequest(title, username, password, website, notes);
        passwordApi.createPassword(request).enqueue(new Callback<Password>() {
            @Override
            public void onResponse(Call<Password> call, Response<Password> response) {
                if (response.isSuccessful()) {
                    Toast.makeText(AddPasswordActivity.this, 
                        "保存成功", Toast.LENGTH_SHORT).show();
                    finish();
                } else {
                    Toast.makeText(AddPasswordActivity.this, 
                        "保存失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<Password> call, Throwable t) {
                Toast.makeText(AddPasswordActivity.this, 
                    "网络错误", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 